# 数据库操作
import os.path
from datetime import datetime
from typing import List

from sqlalchemy import select, func
from sqlalchemy.orm import Session

from api.database.models import SampleEntity, StandradLibEntity, ModelEntity
from api.utils import rootpath


def get_sample(db: Session, sample_name: str, user_name: str):
    sample = db.query(SampleEntity).filter(SampleEntity.sample_name == sample_name
                                           , SampleEntity.user_name == user_name).first()
    return sample


def create_sample_entity(db: Session, sample: SampleEntity):
    exist_sample = get_sample(db, sample.sample_name, sample.user_name)
    if not exist_sample:
        sample.create_time = datetime.now()
        db.add(sample)
        db.commit()
        db.refresh(sample)
        return
    else:
        exist_sample.path = sample.path
        db.commit()
        db.refresh(exist_sample)


# 根据用户名和库名找到真实的库路径
def find_model_path(user_name: str, model_name: str, db: Session):
    model_entity = db.query(ModelEntity).filter(ModelEntity.premodel_name == model_name,
                                                ModelEntity.user_name == user_name).first()
    if not model_entity:
        return None
    path = os.path.join(rootpath(), model_entity.path)
    if not os.path.exists(path):
        return rootpath() + "//model//root//" + model_name
    return path


def sample_list(db: Session, user_name: str):
    if not user_name:
        samples = db.query(SampleEntity).all()
        return samples

    samples = db.query(SampleEntity).filter(SampleEntity.user_name == user_name).all()
    return samples


# 找到标准库定义数据
def find_standrad_text(db: Session, name: str):
    # standrad = db.query(StandradLibEntity).filter(StandradLibEntity.name == name).first()
    # result: list[str] = []
    # if not standrad:
    #     return result

    path = os.path.join(rootpath(), "data", "standrad", "standrad.txt")
    with open(path, 'r', encoding='utf-8') as file:
        lines = file.readlines()
        return lines


# 添加标准库记录
def create_standrad_lib(db: Session, standrad_entity: StandradLibEntity):
    exist_standrad = db.query(StandradLibEntity).filter(StandradLibEntity.name == standrad_entity.name).first()
    if not exist_standrad:
        standrad_entity.create_time = datetime.now()
        db.add(standrad_entity)
        db.commit()
        db.refresh(standrad_entity)
    else:
        return


# 获取标注库列表
def standrad_lib_list(db: Session):
    return db.query(StandradLibEntity).all()


# 根据标准库名获取标注库记录
def standrad_lib_get(db: Session, name: str):
    return db.query(StandradLibEntity).filter(StandradLibEntity.name == name).first()


# 获取模型列表
def models_list(user: str, db: Session):
    count = db.query(ModelEntity).count()
    if count == 0:
        temp_user_name = "admin"
        model_entity = ModelEntity(premodel_name="all-MiniLM-L6-v2", user_name=temp_user_name,
                                   path=f"\\data\\models\\${temp_user_name}\\all-MiniLM-L6-v2")
        db.add(model_entity)
        db.commit()
        db.refresh(model_entity)

    return db.query(ModelEntity).filter(ModelEntity.user_name == user).all()


def create_model(model_entity: ModelEntity, db: Session):
    exist_model = db.query(ModelEntity).filter(ModelEntity.user_name == model_entity.user_name,
                                               ModelEntity.premodel_name == model_entity.premodel_name).first()
    if not exist_model:
        model_entity.create_time = datetime.now()
        db.add(model_entity)
        db.commit()
        db.refresh(model_entity)
    else:
        exist_model.path = model_entity.path
        db.commit()
        db.refresh(exist_model)
        return
